package com.syj.web.service.impl;

import com.syj.common.util.FileUtil;
import com.syj.core.base.service.BaseServiceImpl;
import com.syj.web.dao.ModelInputMapper;
import com.syj.web.dao.ProductModelMapper;
import com.syj.web.dao.WorkDataMapper;
import com.syj.web.dao.WorksMapper;
import com.syj.web.model.ModelInput;
import com.syj.web.model.ProductModel;
import com.syj.web.model.WorkData;
import com.syj.web.model.Works;
import com.syj.web.model.vo.ProductModelVo;
import com.syj.web.service.ProductModelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletRequest;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by rankawin on 2017/2/12.
 */
@Service
public class ProductModelServiceImpl extends BaseServiceImpl<ProductModel> implements ProductModelService {

    @Autowired
    private ProductModelMapper productModelMapper;

    @Autowired
    private ModelInputMapper modelInputMapper;

    @Autowired
    private WorksMapper worksMapper;

    @Autowired
    private WorkDataMapper workDataMapper;

    @Override
    public List<ProductModelVo> selectModel(Integer productModelId) {
        List<ProductModelVo> productModelVos = productModelMapper.selectByVo(productModelId);
        for (ProductModelVo productModelVo : productModelVos) {
            Example example = new Example(ModelInput.class);
            example.orderBy("ons").asc();
            example.createCriteria().andEqualTo("modelId", productModelVo.getId());
            List<ModelInput> select = modelInputMapper.selectByExample(example);
            productModelVo.setModelInputs(select);
        }
        return productModelVos;
    }

    @Override
    public Integer addWorks(MultipartFile[] file, HttpServletRequest request, Integer worksId) {
        List<String> paths = new LinkedList<>();
        if (file != null && file.length > 0) {
            for (MultipartFile multipartFile : file) {
                paths.add(FileUtil.uploadFile(multipartFile, request));
            }
        }
        String productModelId = request.getParameter("productModelId");
        Example example = new Example(ModelInput.class);
        example.orderBy("ons").asc();
        example.createCriteria().andEqualTo("modelId", productModelId);
        List<ModelInput> modelInputs = modelInputMapper.selectByExample(example);
        if (worksId == 0) {
            Works works = new Works();
            works.setProductId(Integer.parseInt(request.getParameter("productId")));
//        works.setUserId(SessionUtil.getUser().getId()); //TODO : 交付使用
            works.setUserId(1);
            worksMapper.insertSelective(works);
            worksId = works.getId();
        }
        int i = 0;
        for (ModelInput modelInput : modelInputs) {
            WorkData workData = new WorkData();
            if (modelInput.getInputType() == 1) {
                workData.setData(paths.get(i));
                i++;
            } else {
                workData.setData(request.getParameter(modelInput.getInputName()));
            }
            workData.setModelInputId(modelInput.getId());
            workData.setWorksId(worksId);
            workDataMapper.insertSelective(workData);
        }
        return worksId;
    }
}
